import { defineStore } from 'pinia'
import { Category, Banner, New, Hot } from '@/types/home'
import { ApiRes } from '@/types/data'
import request from '@/utils/request'

/**
 * 仓库的名字必须唯一
 */
export const useHomeStore = defineStore('home', {
  state: () => {
    return {
      categoryList: [] as Category[], // 分类列表
      bannerList: [] as Banner[],
      newList: [] as New[],
      hotList: [] as Hot[]
    }
  },
  actions: {
    async getCategoryList () {
      const res = await request.get<ApiRes<Category[]>>('/home/category/head')
      this.categoryList = res.data.result
    },
    async getBannerList () {
      const res = await request.get<ApiRes<Banner[]>>('/home/banner')
      this.bannerList = res.data.result
    },
    async getNewList () {
      const res = await request.get<ApiRes<New[]>>('/home/new')
      this.newList = res.data.result
    },
    async getHotList () {
      const res = await request.get<ApiRes<Hot[]>>('/home/hot')
      this.hotList = res.data.result
    }
  }
})
